// Singleton template
#ifndef __SINGLETON_H__
#define __SINGLETON_H__

#include <assert.h>

template<typename T> class Singleton
{
private:
	static T* ms_pSingleton;
public:
	Singleton(void)
	{
		assert(!ms_pSingleton);
#if defined( _MSC_VER ) && _MSC_VER < 1200	 
		int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
		ms_pSingleton = (T*)((int)this + offset);
#else
		ms_pSingleton = static_cast< T* >( this );
#endif
	}

	~Singleton()
	{
		assert(ms_pSingleton);
		ms_pSingleton = 0;
	}

	// static interface
	static T& GetSingleton(void)
	{
		assert(ms_pSingleton);
		return (*ms_pSingleton);
	}

	static T* GetSingletonPtr(void)
	{
		return (ms_pSingleton);
	}
};

template <typename T> T* Singleton<T>::ms_pSingleton = 0;

#endif